package org.example.domain.activity.service.discount;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.activity.adapter.repository.IActivityRepository;
import org.example.domain.activity.model.valobj.DiscounTypeEnum;
import org.example.domain.activity.model.valobj.GroupBuyActivityDiscountVO;

import javax.annotation.Resource;
import java.math.BigDecimal;
@Slf4j
public abstract class AbstractDiscountCalculateService implements IDiscountCalculateService {
    @Resource
    protected IActivityRepository repository;

    @Override
    public BigDecimal calculate(String userId, BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {
        if(DiscounTypeEnum.TAG.equals(groupBuyDiscount.getDiscountType()))
        {
           boolean isCrowdRange= filterTagId(userId,groupBuyDiscount.getTagId());
           if(!isCrowdRange)
           {
               log.info("折扣优惠计算拦截userId:{}",userId);
               return originalPrice;
           }
        }
        return doCalculate(originalPrice,groupBuyDiscount);
    }

    protected abstract BigDecimal doCalculate(BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) ;


    private boolean filterTagId(String userId,String tagId)
    {

        return  repository.isTagCrowRange(tagId,userId);
    }
}
